define(['../../com/base',
       '../../com/mixin',
       'jquery'], function(Base, Mixin, $){
  var Sprite = Base.extend({
    initializer : function(){
      this.bind();
    },

    bind: function(){
      var self = this;

      self.on('add:file', function(args){
        self._parseFileData(args.files);
      });
    },

    _parseFileData: function(files){
      var self = this;
      var fr, result={};

      for(var i = 0; i < files.length; ++i){
        if(self._isImage(files[i])){
          fr = new FileReader();

          var name = files[i].name;
          var extIdx = name.indexOf('.');
          if(extIdx !== -1)name = name.slice(0, extIdx);

          result[i] = {
            name: name
          };
          fr.onload = (function(i){
            return function(e){
              var size = self._getImageSize(e.target.result);
              result[i].data = e.target.result;
              result[i] = Mixin(result[i], size);
              self.fire('add:to:canvas', {
                data : result[i]
              });
            };
          })(i);
          fr.readAsDataURL(files[i]);
        }
      }
    },

    _getImageSize: function(fileData){
      var imgDom = $('<img src="' + fileData + '" style="display:none;" />');
      $('body').append(imgDom);
      var size = {
        width  : imgDom.width(),
        height : imgDom.height()
      };
      imgDom.remove();

      return size;
    },

    _isImage: function(file){
      var imageReg = /^image.*/;
return file.type.match(imageReg);
    }
  }, {
    files:{
      value: []
    }
  });

  return Sprite;
});
